package com.yang.crawler.article.executor;

import com.yang.crawler.CrawlColumnCommand;
import com.yang.crawler.common.BusinessException;
import com.yang.crawler.common.ResultCode;
import com.yang.crawler.dto.data.ArticleColumnDTO;
import com.yang.crawler.dto.data.ArticleDTO;
import com.yang.crawler.dto.data.ArticleLinkDTO;
import com.yang.crawler.dto.data.ColumnDTO;
import com.yang.crawler.strategy.IArticleCrawlStrategy;
import com.yang.crawler.strategy.dto.ArticleColumnCrawlDTO;
import com.yang.crawler.strategy.factory.ArticleCrawlStrategyFactory;
import com.yang.crawler.strategy.request.ArticleColumnCrawlRequest;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class CrawColumnCmdExecutor {
    public ColumnDTO execute(CrawlColumnCommand crawlColumnCommand) {
        String website = crawlColumnCommand.getWebsite();
        String columnUrl = crawlColumnCommand.getColumnUrl();
        if (website == null) {
            int index = columnUrl.indexOf("//");
            int start = index + 2;
            int end = columnUrl.indexOf("/", start);
            website = columnUrl.substring(start, end);
        }
        IArticleCrawlStrategy articleCrawlStrategy = ArticleCrawlStrategyFactory.getArticleCrawlStrategy(website);
        if (articleCrawlStrategy == null) {
            throw new BusinessException(ResultCode.WEBSITE_NOT_SUPPORT);
        }
        ArticleColumnCrawlRequest articleColumnCrawlRequest = new ArticleColumnCrawlRequest();
        articleColumnCrawlRequest.setColumnUrl(columnUrl);
        ArticleColumnCrawlDTO articleColumnCrawlDTO = articleCrawlStrategy.crawlArticleColumn(articleColumnCrawlRequest);

        ArticleColumnDTO articleColumnDTO = convert2ArticleColumnDTO(articleColumnCrawlDTO);
        return convert2ColumnDTO(articleColumnDTO);
    }

    private ArticleColumnDTO convert2ArticleColumnDTO(ArticleColumnCrawlDTO articleColumnCrawlDTO) {
        ArticleColumnDTO articleColumn = new ArticleColumnDTO();
        articleColumn.setWebsite(articleColumnCrawlDTO.getWebsite());
        articleColumn.setProtocol(articleColumnCrawlDTO.getProtocol());
        articleColumn.setUrl(articleColumnCrawlDTO.getUrl());
        articleColumn.setTitle(articleColumnCrawlDTO.getTitle());
        for (Map.Entry<String, String> entry : articleColumnCrawlDTO.getArticleTitleLinkMap().entrySet()) {
            String articleTitle = entry.getKey();
            String articleLink = entry.getValue();
            ArticleLinkDTO articleLinkDTO = new ArticleLinkDTO();
            articleLinkDTO.setTitle(articleTitle);
            articleLinkDTO.setUrl(articleLink);

            articleColumn.addArticleLink(articleLinkDTO);
        }
        return articleColumn;
    }

    private ColumnDTO convert2ColumnDTO(ArticleColumnDTO articleColumnDTO) {
        ColumnDTO columnDTO = new ColumnDTO();
        columnDTO.setWebsite(articleColumnDTO.getWebsite());
        columnDTO.setColumnUrl(articleColumnDTO.getUrl());
        columnDTO.setTitle(articleColumnDTO.getTitle());
        for (ArticleLinkDTO articleLinkDTO : articleColumnDTO.getArticleLinkDTOList()) {
            ArticleDTO articleDTO = new ArticleDTO();
            articleDTO.setTitle(articleLinkDTO.getTitle());
            articleDTO.setArticleUrl(articleLinkDTO.getUrl());
            columnDTO.addArticleDTO(articleDTO);
        }
        columnDTO.setArticleColumnDTO(articleColumnDTO);
        return columnDTO;
    }
}
